package guida.ccf.gzuexaminesystem.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;


// 只有在字段的值不为null时，才会被包含在生成的JSON字符串中。
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 提示信息，如果有错误时，前端可以获取该字段进行提示
     */
    private String msg;
    /**
     * 查询到的结果数据，
     */
    private T data;

    public static <T> ResponseResult<T> success(T data) {

        ResponseResult<T> result = new ResponseResult<T>();

        result.code = HttpStatus.OK;
        result.msg = "操作成功";
        result.data = data;

        return result;
    }


    public static <T> ResponseResult<T> fail(String msg) {
        ResponseResult<T> result = new ResponseResult<>();

        result.msg = msg;
        result.code = HttpStatus.FAILED;

        return result;
    }

    public static <T> ResponseResult<T> fail(String msg, Integer code) {
        ResponseResult<T> result = new ResponseResult<>();

        result.msg = msg;
        result.code = code;

        return result;
    }


    public static <T> ResponseResult<T> failUnauthorized(String msg) {
        ResponseResult<T> result = new ResponseResult<>();

        result.code = HttpStatus.UNAUTHORIZED;
        result.msg = msg;

        return result;
    }

    public static <T> ResponseResult<T> failRedirect(String msg) {
        ResponseResult<T> result = new ResponseResult<>();

        result.code = HttpStatus.REDIRECT;
        result.msg = msg;

        return result;
    }



}